package com.bwei.topnews;

import android.content.Intent;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.bwei.topnews.adapter.LocalCityAdapter;
import com.bwei.topnews.bean.CityInfo;
import com.bwei.topnews.manager.DBManager;
import com.bwei.topnews.view.letterView;

import java.io.File;
import java.util.HashMap;
import java.util.List;

public class LocalCityA extends AppCompatActivity {

    private ListView listView;
    private letterView view_letter;
    private HashMap<String,Integer> map = new HashMap<>();
    private List<CityInfo> cityList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.localcityacitivity);
        listView = (ListView) findViewById(R.id.lv_local_city);
        view_letter = (letterView) findViewById(R.id.view_letter);
        view_letter.setOnLetterChangeListener(new letterView.OnLetterChanggeListener() {
            @Override
            public void setOnLetterChangge(String letter) {
                try {
                    listView.setSelection(map.get(letter));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        DBManager db  = new DBManager(this);
        db.copy();
        cityList = db.getCityList();

        for (int i = 0; i < cityList.size(); i++) {
            if(cityList.get(i).isFlag()){
                map.put(cityList.get(i).getNamesort(),i);
            }
        }
        listView.setAdapter(new LocalCityAdapter(cityList,this));

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String cityname = cityList.get(position).getCityname();
                Intent in = new Intent();
                in.putExtra("city",cityname);
                setResult(101,in);
                finish();

            }
        });

        
    }
}
